package ar.edu.itba.tp1.tpmariano;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;

public class GameTile extends JPanel {

	private static final long serialVersionUID = 1L;

	public GameTile(int x, int y, char tile) {
		setSize(100, 100);
		setLayout(null);
		setBackground(Color.gray);
		setBorder(LineBorder.createBlackLineBorder());

		initialize(x, y, tile);
	}

	public JLabel getColor(int rot, int color) {
		JLabel label = new JLabel();
		try {
			if (color != 0) {
				Image image = ImageIO.read(new File("images/c_" + color + "_" + rot + ".png"));
				label.setIcon(new ImageIcon(image));
			}
		} catch (IOException e) {
			System.err.println("Could not load tile image!");
		}
		return label;
	}

	public void initialize(int x, int y, char tile) {

		JLabel text = new JLabel("(" + x + "," + y + ")");

		text.setFont(new Font("SansSerif", Font.BOLD, 16));
		text.setForeground(Color.white);
		text.setBounds(35, 23, 50, 50);
		add(text);

		// UP
		JLabel label = getColor(0, (tile >> 12) & 0xF);
		label.setBounds(0, 0, 100, 100);
		add(label);

		// DOWN
		JLabel label2 = getColor(2, (tile >> 4) & 0xF);
		label2.setBounds(0, 0, 100, 100);
		add(label2);

		// LEFT
		JLabel label3 = getColor(3, tile & 0xF);
		label3.setBounds(0, 0, 100, 100);
		add(label3);

		// RIGHT
		JLabel label4 = getColor(1, (tile >> 8) & 0xF);
		label4.setBounds(0, 0, 100, 100);
		add(label4);

	}
}
